var models = require('../models');
var NotificationReply = models.NotificationReply;
var EventProxy = require('eventproxy');

var tools = require('../common/tools');
var User = require('./user');
var at = require('../common/at');

/**
 * 获取一条回复信息
 * @param {String} id 回复ID
 * @param {Function} callback 回调函数
 */
exports.getNotificationReply = function (id, callback) {
  NotificationReply.findOne({_id: id}, callback);
};

/**
 * 根据回复ID，获取回复
 * Callback:
 * - err, 数据库异常
 * - notificationReply, 回复内容
 * @param {String} id 回复ID
 * @param {Function} callback 回调函数
 */
exports.getNotificationReplyById = function (id, callback) {
  NotificationReply.findOne({_id: id}, function (err, notificationReply) {
    if (err) {
      return callback(err);
    }
    if (!notificationReply) {
      return callback(err, null);
    }

    var author_id = notificationReply.author_id;
    User.getUserById(author_id, function (err, author) {
      if (err) {
        return callback(err);
      }
      notificationReply.author = author;
      notificationReply.friendly_create_at = tools.formatDate(notificationReply.create_at, true);
      // TODO: 添加更新方法，有些旧帖子可以转换为markdown格式的内容
      if (notificationReply.content_is_html) {
        return callback(null, notificationReply);
      }
      at.linkUsers(notificationReply.content, function (err, str) {
        if (err) {
          return callback(err);
        }
        notificationReply.content = str;
        return callback(err, notificationReply);
      });
    });
  });
};

/**
 * 根据主题ID，获取回复列表
 * Callback:
 * - err, 数据库异常
 * - replies, 回复列表
 * @param {String} id 主题ID
 * @param {Function} callback 回调函数
 */
exports.getRepliesByNotificationId = function (id, cb) {
  NotificationReply.find({notification_id: id}, '', {sort: 'create_at'}, function (err, replies) {
    if (err) {
      return cb(err);
    }
    if (replies.length === 0) {
      return cb(null, []);
    }

    var proxy = new EventProxy();
    proxy.after('notificationReply_find', replies.length, function () {
      cb(null, replies);
    });
    for (var j = 0; j < replies.length; j++) {
      (function (i) {
        var author_id = replies[i].author_id;
        User.getUserById(author_id, function (err, author) {
          if (err) {
            return cb(err);
          }
          replies[i].author = author || { _id: '' };
          replies[i].friendly_create_at = tools.formatDate(replies[i].create_at, true);
          if (replies[i].content_is_html) {
            return proxy.emit('notificationReply_find');
          }
          at.linkUsers(replies[i].content, function (err, str) {
            if (err) {
              return cb(err);
            }
            replies[i].content = str;
            proxy.emit('notificationReply_find');
          });
        });
      })(j);
    }
  });
};

/**
 * 创建并保存一条回复信息
 * @param {String} content 回复内容
 * @param {String} notificationId 主题ID
 * @param {String} authorId 回复作者
 * @param {String} [notificationReplyId] 回复ID，当二级回复时设定该值
 * @param {Function} callback 回调函数
 */
exports.newAndSave = function (content, notificationId, authorId, notificationReplyId, callback) {
  if (typeof notificationReplyId === 'function') {
    callback = notificationReplyId;
    notificationReplyId = null;
  }
  var notificationReply = new NotificationReply();
  notificationReply.content = content;
  notificationReply.notification_id = notificationId;
  notificationReply.author_id = authorId;
  if (notificationReplyId) {
    notificationReply.notificationReply_id = notificationReplyId;
  }
  notificationReply.save(function (err) {
    callback(err, notificationReply);
  });
};

exports.getRepliesByAuthorId = function (authorId, opt, callback) {
  if (!callback) {
    callback = opt;
    opt = null;
  }
  NotificationReply.find({author_id: authorId}, {}, opt, callback);
};

// 通过 author_id 获取回复总数
exports.getCountByAuthorId = function (authorId, callback) {
  NotificationReply.count({author_id: authorId}, callback);
};
